package com.qolome.utils.format;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.http.client.utils.DateUtils;

/**
 * 
 * @Description:  日期处理
 * @author 姜锋
 * @date 2019年4月7日 上午10:28:55 
 * @version V1.0   
 *
 */
public class GingerDateUtils {
	
	/** 日期格式(yyyy-MM-dd) */
	public final static String DATE_PATTERN = "yyyy-MM-dd";
	/** 日期时间格式(yyyy-MM-dd HH:mm:ss) */
	public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
	/** 日期时间毫秒格式yyyy-MM-dd HH:mm:ss.SSS) */
	public final static String DATE_TIME_MILLISECOND_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS";
	
	public static String format(Date date) {
        return format(date, DATE_TIME_PATTERN);
    }

    public static String format(Date date, String pattern) {
    	return date != null ? DateUtils.formatDate(date, pattern) : null;
    }
    
    /**
	 * @function:如果前者早于后者则返回true
	 * @param beforeTime
	 * @param afterTime
	 * @return
	 * @author: llh 2008-7-12 下午01:19:07
	 */
	public static boolean compareTime(Calendar beforeTime, Calendar afterTime)
	{
		boolean flag = false;
		if (beforeTime.before(afterTime))
		{
			flag = true;
		}
		return flag;
	}

	
	/**
	 * @function:将字符串转为Calendar类型
	 * @param dateTime 默认：当前系统时间
	 * @param formatString 默认：yyyy-MM-dd
	 * @return
	 * @author: llh 2008-7-2 上午09:27:41
	 */
	public static Calendar formatDateFromStringToCalendar(String dateTime, String formatString)
	{
		Calendar cal = Calendar.getInstance();
		if (dateTime == null || "".equals(dateTime))
		{
			cal.setTime(new Date());
		} else
		{
			if (formatString == null || "".equals(formatString))
			{
				formatString = "yyyy-MM-dd";
			}
			SimpleDateFormat df = new SimpleDateFormat(formatString);
			try
			{
				cal.setTime(df.parse(dateTime));
			} catch (ParseException e)
			{
				e.printStackTrace();
			}
		}
		if (formatString == null)
		{
			cal.set(Calendar.HOUR_OF_DAY, 0);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.SECOND, 0);
			cal.set(Calendar.MILLISECOND, 0);
		}
		return cal;
	}
	
	
	
	public static Date strToDate(String str) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_PATTERN);
        return sdf.parse(str);
	}
	
	public static Date strToDate(String str, String paeeern) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(paeeern);
        return sdf.parse(str);
	}


}
